package com.daocao.ds.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.allen.library.SuperButton;
import com.daocao.ds.R;
import com.daocao.ds.adapter.MyOrderAdapter;
import com.daocao.ds.api.Api;
import com.daocao.ds.api.bean.ManagerListBean;
import com.daocao.ds.constant.UserInfoGlobal;
import com.daocao.ds.event.ChangPageEvent;
import com.daocao.ds.event.MainChangePageEvent;
import com.daocao.ds.util.Network;
import com.daocao.ds.util.RxLifeUtil;
import com.daocao.ds.util.ToastUtil;
import com.daocao.ds.view.OnLoadeMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-11-16.
 */

public class MyItemOrderFragment extends BaseFragment {
    @BindView(R.id.btn_pay)
    SuperButton btnPay;
    @BindView(R.id.layout_no_data)
    LinearLayout layoutNoData;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.layout_bottom)
    LinearLayout layoutBottom;
    @BindView(R.id.layout_has_data)
    LinearLayout layoutHasData;
    Unbinder unbinder;
    @BindView(R.id.btn_turn_top)
    ImageView btnTurnTop;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    private MyOrderAdapter adapter;
    //    private String date;
    //      "has_repay": 0，  //是否已回款，默认为 -1 ： -1 全部、 0 未回款、1 已回款
    //              "is_overdue": 0，  //是否逾期，默认为-1 ： -1 全部、0 否、1-是
    private String has_repay = "-1";
    private String is_overdue;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getManagerList(page);
        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my_item_order;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        adapter = new MyOrderAdapter(getContext());
        list.setAdapter(adapter);

        int id = getArguments().getInt("id", 0);

        switch (id) {
            case 0:
                has_repay = "-1";
                break;
            case 1:
                has_repay = "0";
                break;
            case 2:
                has_repay = "1";
                break;
            case 3:
                has_repay = "0";
                is_overdue = "1";
                break;
        }

//        List<String> data = new ArrayList<>();
//        data.add("");
//        data.add("");
//        data.add("");
//        data.add("");
//        adapter.update(data);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getManagerList(1);
            }
        });
        onLoadeMoreListener.setPage();
        getManagerList(1);
    }

    public void getManagerList(int page) {
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.makeText(getContext(), "网络连接失败").show();
            return;
        }
        refresh.setRefreshing(true);
        ManagerListBean bean = new ManagerListBean();
        bean.page = page;
        bean.is_overdue = is_overdue;
        bean.has_repay = has_repay;
        bean.date = UserInfoGlobal.date;
        Api.api_service.getManagerList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if(adapter.getItemCount()==0){
                        layoutNoData.setVisibility(View.VISIBLE);
                        layoutHasData.setVisibility(View.GONE);
                    }else {
                        layoutNoData.setVisibility(View.GONE);
                        layoutHasData.setVisibility(View.VISIBLE);
                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MainChangePageEvent event) {
        onLoadeMoreListener.setPage();
        getManagerList(1);
    }

    @OnClick({R.id.btn_pay, R.id.btn_turn_top})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_pay:
                EventBus.getDefault().post(new ChangPageEvent(0));
                break;
            case R.id.btn_turn_top:
                list.scrollToPosition(0);
                break;
        }
    }
}
